﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMN.Entity;
using CMN.Common;
using CMN.Framework;
using CMN.BLL;
using System.Diagnostics;
using CMN.Config;
using Newtonsoft.Json;
namespace CMN.UI
{
    public class TemplateDisplay
    {
        public TemplateDisplay()
        {

        }


        #region 分页
        public QueryParam qp = new QueryParam();

        public QueryParam GetPageInfo()
        {
            return qp;
        }


        #region 分页程序

        /// <summary>
        /// 分页程序
        /// </summary>
        /// <param name="recordCount"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strPageName">格式化连接字符串如(class.aspx?id={0})</param>
        /// <returns></returns>
        public List<PagingInfo> GetPagingList(int showNum,int maxCount, string strPageName)
        {
            if( qp.Count>maxCount)
                qp.Count = maxCount;
            return GetPagingList(showNum, strPageName, "", true);
        }

         /// <summary>
        /// 分页程序
        /// </summary>
        /// <param name="recordCount"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strPageName">格式化连接字符串如(class.aspx?id={0})</param>
        /// <returns></returns>
        public List<PagingInfo> GetPagingList(int showNum, string strPageName,string strFirstUrl)
        {
            return GetPagingList(showNum, strPageName, strFirstUrl, true);
        }

        /// <summary>
        /// 分页程序
        /// </summary>
        /// <param name="recordCount"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strPageName">格式化连接字符串如(class.aspx?id={0})</param>
        /// <returns></returns>
        public List<PagingInfo> GetPagingList(int showNum, string strPageName, string strFirstUrl,bool isShowHz)
        {

            SiteConfigInfo siteInfo= SiteConfigs.GetSiteConfig();
            if (siteInfo.MaxPageNum != 0 && !string.IsNullOrEmpty(qp.ChannelId))
            {
                if (qp.Count > (siteInfo.MaxPageNum * qp.PageSize))
                {
                    qp.Count = siteInfo.MaxPageNum * qp.PageSize;
                    if (qp.PageIndex > siteInfo.MaxPageNum)
                        qp.PageIndex = siteInfo.MaxPageNum;
                }
            }
            int pageNum = qp.PageCount;
            List<PagingInfo> pagingInfoList = new List<PagingInfo>();
            if (qp.PageIndex > qp.PageCount)
            {
                return pagingInfoList;
            }
            if (string.IsNullOrEmpty(strFirstUrl))
                strFirstUrl = string.Format(strPageName, "1");
            PagingInfo pagingInfo = null;
            int showHelf = showNum / 2;
            //总页数小于展示页数
            if (pageNum <= showNum)
            {

                for (int i = 1; i <= pageNum; i++)
                {
                    if (i == qp.PageIndex)
                    {
                        pagingInfo = new PagingInfo();
                        pagingInfo.Href = "";
                        pagingInfo.Title = i.ToString();
                        pagingInfoList.Add(pagingInfo);
                    }
                    else
                    {
                        //第一页
                        if (i == 1)
                        {
                            pagingInfo = new PagingInfo();
                            if (!string.IsNullOrEmpty(strFirstUrl))
                                pagingInfo.Href = strFirstUrl;
                            else
                             pagingInfo.Href = string.Format(strPageName, i.ToString());
                            
                           
                            pagingInfo.Title = i.ToString();
                            pagingInfoList.Add(pagingInfo);
                        }
                        else
                        {
                            pagingInfo = new PagingInfo();
                            pagingInfo.Href = string.Format(strPageName, i.ToString());
                            pagingInfo.Title = i.ToString();
                            pagingInfoList.Add(pagingInfo);
                        }
                    }
                }

            }
            else
            {
                if (qp.PageIndex > 1)
                {
                    if (isShowHz)
                    {
                        pagingInfo = new PagingInfo();
                        pagingInfo.Href = strFirstUrl;
                        pagingInfo.Title = "首页";
                        pagingInfoList.Add(pagingInfo);
                        pagingInfo = new PagingInfo();
                        pagingInfo.Href = string.Format(strPageName, (qp.PageIndex - 1).ToString());
                        pagingInfo.Title = "上一页";
                        pagingInfoList.Add(pagingInfo);
                    }
                }

                if (((qp.PageIndex + showHelf) <= pageNum) && ((qp.PageIndex - showHelf) > 0))
                {
                    for (int i = (qp.PageIndex - showHelf); i <= (qp.PageIndex + showHelf); i++)
                    {
                        if (i == qp.PageIndex)
                        {
                            pagingInfo = new PagingInfo();
                            pagingInfo.Href = "";
                            pagingInfo.Title = i.ToString();
                            pagingInfoList.Add(pagingInfo);
                        }
                        else
                        {
                            //第一页
                            if (i == 1)
                            {
                                pagingInfo = new PagingInfo();
                                if (!string.IsNullOrEmpty(strFirstUrl))
                                    pagingInfo.Href = strFirstUrl;
                                else
                                    pagingInfo.Href = string.Format(strPageName, i.ToString());
                                pagingInfo.Title = i.ToString();
                                pagingInfoList.Add(pagingInfo);
                            }
                            else
                            {
                                pagingInfo = new PagingInfo();
                                pagingInfo.Href = string.Format(strPageName, i.ToString());
                                pagingInfo.Title = i.ToString();
                                pagingInfoList.Add(pagingInfo);
                            }
                        }
                    }
                }
                else
                {
                    if ((qp.PageIndex + showHelf) >= pageNum)
                    {
                        for (int i = (pageNum - showNum); i < pageNum; i++)
                        {
                            if (i == qp.PageIndex)
                            {
                                pagingInfo = new PagingInfo();
                                pagingInfo.Href = "";
                                pagingInfo.Title = i.ToString();
                                pagingInfoList.Add(pagingInfo);
                            }
                            else
                            {
                                //第一页
                                if (i == 1)
                                {
                                    pagingInfo = new PagingInfo();
                                    if (!string.IsNullOrEmpty(strFirstUrl))
                                        pagingInfo.Href = strFirstUrl;
                                    else
                                        pagingInfo.Href = string.Format(strPageName, i.ToString());
                                    pagingInfo.Title = i.ToString();
                                    pagingInfoList.Add(pagingInfo);
                                }
                                else
                                {
                                    pagingInfo = new PagingInfo();
                                    pagingInfo.Href = string.Format(strPageName, i.ToString());
                                    pagingInfo.Title = i.ToString();
                                    pagingInfoList.Add(pagingInfo);
                                }
                            }
                        }

                    }
                    else
                    {
                        for (int i = 1; i < showNum; i++)
                        {
                            if (i == qp.PageIndex)
                            {
                                pagingInfo = new PagingInfo();
                                pagingInfo.Href = "";
                                pagingInfo.Title = i.ToString();
                                pagingInfoList.Add(pagingInfo);
                            }
                            else
                            {
                                //第一页
                                if (i == 1)
                                {
                                    pagingInfo = new PagingInfo();
                                    if (!string.IsNullOrEmpty(strFirstUrl))
                                        pagingInfo.Href = strFirstUrl;
                                    else
                                        pagingInfo.Href = string.Format(strPageName, i.ToString());
                                    pagingInfo.Title = i.ToString();
                                    pagingInfoList.Add(pagingInfo);
                                }
                                else
                                {
                                    pagingInfo = new PagingInfo();
                                    pagingInfo.Href = string.Format(strPageName, i.ToString());
                                    pagingInfo.Title = i.ToString();
                                    pagingInfoList.Add(pagingInfo);
                                }
                            }
                        }
                    }
                }


                if (qp.PageIndex < pageNum)
                {
                    if (isShowHz)
                    {
                        pagingInfo = new PagingInfo();
                        pagingInfo.Href = string.Format(strPageName, (qp.PageIndex + 1).ToString());
                        pagingInfo.Title = "下一页";
                        pagingInfoList.Add(pagingInfo);
                        pagingInfo = new PagingInfo();
                        pagingInfo.Href = string.Format(strPageName, pageNum.ToString());
                        pagingInfo.Title = "末页";
                        pagingInfoList.Add(pagingInfo);

                    }

                }
            }

            return pagingInfoList;
        }

        /// <summary>
        /// 分页程序
        /// </summary>
        /// <param name="recordCount"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strPageName">格式化连接字符串如(class.aspx?id={0})</param>
        /// <returns></returns>
        public List<PagingInfo> GetPagingList(int showNum, string strPageName)
        {
            return GetPagingList(showNum, strPageName, "", true);
        }
        #endregion

        #endregion

        #region 频道列表
        public List<Normal_ChannelEntity> ChannelList()
        {
            return MvcWebCacheManager.GetNormal_ChannelList();
        }
        public List<Normal_ChannelEntity> ChannelList(string strWhere ,string orderBy ,int pageSize)
        {
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = pageSize;
            qp.Orderfld = orderBy;
            qp.Where = strWhere;
            return MvcWebCacheManager.GetNormal_ChannelList(qp);
        }


        public List<Normal_ChannelEntity> ChannelList(int channelId)
        {
            return MvcWebCacheManager.GetNormal_ChannelList(channelId,100);
        }
        public List<Normal_ChannelEntity> ChannelList(int channelId,int retrunCount)
        {
            return MvcWebCacheManager.GetNormal_ChannelList(channelId, retrunCount);
        }
        /// <summary>
        /// 获取栏目列表，如果没有子栏目则回去同级栏目
        /// </summary>
        /// <param name="channelId"></param>
        /// <returns></returns>
        public List<Normal_ChannelEntity> ChannelList2(int channelId)
        {
            return MvcWebCacheManager.GetNormal_ChannelList2(channelId);
        }

        

        public List<Normal_ChannelEntity> ChannelList(string dir)
        {
            Normal_ChannelEntity channelEntity = MvcWebCacheManager.GetNormal_ChannelByDir(dir);
            if (channelEntity == null)
            {
                return new List<Normal_ChannelEntity>();
            }
            return MvcWebCacheManager.GetNormal_ChannelList(channelEntity.Id,1000);
        }
        /// <summary>
        /// 获取推荐频道列表
        /// </summary>
        /// <param name="channelPath"></param>
        /// <param name="isRecommend"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<Normal_ChannelEntity> ChannelList(string channelPath, bool isRecommend, int count)
        {
            return MvcWebCacheManager.GetNormal_ChannelList(channelPath,isRecommend,count);
        }
        public Normal_ChannelEntity GetChannel(int id)
        {
            return MvcWebCacheManager.GetNormal_Channel(id);
        }
        public Normal_ChannelEntity GetChannel(string dir)
        {
            return MvcWebCacheManager.GetNormal_ChannelByDir(dir);
        }
        public Normal_ChannelEntity GetChannelByPath(string path)
        {
            return MvcWebCacheManager.GetNormal_ChannelByPath(path);
        }
        #endregion

        #region ModuleList 内容列表
        

        

        public List<Module_ListInfo> TopList(string channelId, int pageSize, string type, bool isAll, int cacheSecends)
        {
            string strWhere = string.Concat("toptime >'", DateTime.Now.AddDays(-100).ToString("yyyyMMdd"), "'");
            List<Module_ListInfo> mList = ModuleList(channelId, pageSize, 1, type, strWhere, "adddate desc ", isAll, false, cacheSecends, false);
            return mList;
        }

        //public List<Module_ListInfo> FlagList(string channelId, int pageSize, string type, string flag, bool isAll, int cacheSecends)
        //{
        //    string strWhere = string.Concat("flag='", flag, "'");
        //    List<Module_ListInfo> mList = ModuleList(channelId, pageSize, 1, type, strWhere, "adddate desc ", isAll, false, cacheSecends, false);
        //    return mList;
        //}
     
        public List<Module_ListInfo> ModuleList(string channelId, int pageSize, string type, string order,bool isAll, int cacheSecends)
        {
            List<Module_ListInfo> mList = ModuleList(channelId, pageSize, 1, type, "", order, isAll, false, cacheSecends, false);
            return mList;
        }


        public List<Module_ListInfo> ModuleList(string channelId, int pageSize, string type, string strWhere, string order, bool isAll, int cacheSecends)
        {
            List<Module_ListInfo> mList = ModuleList(channelId, pageSize, 1, type, strWhere, order, isAll, false, cacheSecends, false);
            return mList;
        }
        public List<Module_ListInfo> ModuleList(string channelId, int pageSize, string type, string strWhere, string order, bool isAll,bool isImg, int cacheSecends)
        {
            List<Module_ListInfo> mList = ModuleList(channelId, pageSize, 1, type, strWhere, order, isAll, isImg, cacheSecends, false);
            return mList;
        }

        public List<Module_ListInfo> ModuleList(string channelId, int pageSize, string type, int cacheSecends, string rankingType, string dateType)
        {
            return ModuleList(channelId, pageSize, type, cacheSecends, rankingType, dateType, true,false);
        }

        /// <summary>
        /// 获取内容排行列表
        /// </summary>
        /// <param name="channelId">栏目Id可为空</param>
        /// <param name="pageSize">返回数</param>
        /// <param name="type">内容类型</param>
        /// <param name="cacheSecends">缓存时间</param>
        /// <param name="rankingType">排行类型（click=点击排行；support=好评排行;against=差评排行)</param>
        /// <param name="dateType">返回时间类型day,week,month,season,year</param>
        /// <returns></returns>
        public List<Module_ListInfo> ModuleList(string channelId, int pageSize, string type, int cacheSecends, string rankingType, string dateType,bool isAll,bool isImg)
        {
            string strWhere = string.Empty;
            string strWhereDate = string.Empty;
            if (string.IsNullOrEmpty(rankingType) || string.IsNullOrEmpty(dateType))
            {
                throw new Exception("模板排行榜查询类型出错");
            }

            DateTime dt = DateTime.Now; //当前时间


            DateTime startTime;
            DateTime endTime;

            switch (dateType.ToLower())
            {
                case "day":
                    {
                        startTime = new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0);
                        endTime = new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59);
                        break;
                    }
                case "week":
                    {
                        startTime = dt.AddDays(0 - Convert.ToInt32(dt.DayOfWeek.ToString("d"))); //本周周一
                        startTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, 0, 0, 0);
                        endTime = startTime.AddDays(6); //本周周日
                        break;
                    }
                case "month":
                    {
                        startTime = dt.AddDays(1 - dt.Day); //本月月初
                        startTime = new DateTime(startTime.Year, startTime.Month, startTime.Day,0, 0, 0);
                        endTime = startTime.AddMonths(1).AddDays(-1); //本月月末
                        break;
                    }
                case "season":
                    {
                        startTime = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day); //本季度初
                        startTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, 0, 0, 0);
                        endTime = startTime.AddMonths(3).AddDays(-1); //本季度末
                        break;
                    }
                case "year":
                    {
                        startTime = new DateTime(dt.Year, 1, 1, 0, 0, 0); //本年年初
                        endTime = new DateTime(dt.Year, 12, 31, 23, 59, 59); //本年年末
                        break;
                    }
                default:
                    {
                        throw new Exception("内容排行列表模板日期类型错误");
                    }
            }
            BLLHelper bll = new BLLHelper();
             QueryParam qpIn = new QueryParam();
            string table= string.Concat("Normal_", rankingType.Substring(0, 1).ToUpper(), rankingType.Substring(1, rankingType.Length - 1), "Record");
            if (!string.IsNullOrEmpty(channelId))
            {
                Normal_ChannelEntity channel = MvcWebCacheManager.GetNormal_Channel(Utils.StringToInt(channelId, 0));
                if (channel != null)
                {
                    strWhereDate = string.Concat(" ModuleType like '", channel.Path, "%' and   ",table,".adddate>", bll.GetSqlDateTime(startTime), " and  ",table,".adddate<", bll.GetSqlDateTime(endTime));
                }
            }
            if (string.IsNullOrEmpty(strWhereDate))
            {
                strWhereDate = string.Concat(" ", table,".adddate>", bll.GetSqlDateTime(startTime), " and  ",table,".adddate<", bll.GetSqlDateTime(endTime));
            }
           
            if (cacheSecends > 0)
            {
                qpIn.IsTimeSearch = true;
            }
            qpIn.Orderfld = "";
            qpIn.PageSize = pageSize;
            qpIn.PageIndex = 1;
           qpIn.ReturnFields = "Module_Article.Id,ChannelId,Path ,Title,Module_Article.AddDate,Summary,Tags,Img ,SourceFrom,ViewNum,SupportNum";
           qpIn.TableName = string.Concat( table, "    inner  join  Module_Article    on Module_Article.id=", table, ".id  ");
           qpIn.Where = string.Concat(strWhereDate, " group by ", table, ".id ");
            qpIn.Orderfld = string.Concat("  count(",table,".id)  desc ");
            

            List<Module_ListInfo> List = MvcWebCacheManager.GetModuleList("article", qpIn, cacheSecends, false);
            return List;
        }


      

        /// <summary>
        /// 获取内容列表
        /// </summary>
        /// <param name="channelId">频道id 可多个用英文逗号分隔如 (1,2,3)</param>
        /// <param name="pageSize">显示个数</param>
        /// <param name="pageIndex">当前页数</param>
        /// <param name="type">内容类型 article,soft,video,photo</param>
        /// <param name="strWhere">查询语句</param>
        /// <param name="order">排序 如：adddate desc</param>
        /// <param name="isAll">是否包含子频道</param>
        /// <param name="isImg">是否推荐图片</param>
        /// <param name="cacheSecends">缓存时间 0为不缓存</param>
        /// <param name="isReturnCountNum">是否返回记录总数</param>
        /// <returns></returns>
        public List<Module_ListInfo> ModuleList(string channelId, int pageSize, int pageIndex, string type, string strWhere, string order, bool isAll, bool isImg, int cacheSecends, bool isReturnCountNum)
        {
            QueryParam qpTemp = new QueryParam();
            

            qpTemp = new QueryParam();
            qpTemp.PageSize = pageSize;
            if (pageIndex < 1)
            {
                pageIndex = 1;
            }
            qpTemp.PageIndex = pageIndex;
            qpTemp.Where = " Enabled=1 and IsPass=1";
            if (!string.IsNullOrEmpty(channelId) && channelId != "0") //某频道下的
            {
                qpTemp.ChannelId = channelId;
                if (SiteConfigs.GetSiteConfig().MaxPageNum != 0 && qpTemp.PageIndex > SiteConfigs.GetSiteConfig().MaxPageNum)
                    qpTemp.PageIndex = SiteConfigs.GetSiteConfig().MaxPageNum;
                if (channelId.IndexOf(",") > -1)  //是否是多个id
                {
                    qpTemp.Where = string.Concat("  ChannelId in (", channelId, ") and ", qpTemp.Where);
                }
                else
                {
                    int intChannelId = Utils.StringToInt(channelId, 0);
                    Normal_ChannelEntity channelEntity = MvcWebCacheManager.GetNormal_Channel(intChannelId);
                    if (channelEntity == null)
                    {
                        return new List<Module_ListInfo>();
                    }
                    if (isAll)
                    {

                        qpTemp.Where = string.Concat( "  path like '", channelEntity.Path, "%' and ",qpTemp.Where);

                    }
                    else
                    {
                        qpTemp.Where = string.Concat( "  ChannelId =", channelId," and ",qpTemp.Where);
                    }
                }
            }


            if (isImg)  //是否推荐图片
            {

                qpTemp.Where = string.Concat(qpTemp.Where, " and  isimg=1 ");

            }
            if (!string.IsNullOrEmpty(strWhere))
            {

                qpTemp.Where = string.Concat(qpTemp.Where, " and ", strWhere);

            }
            else
            {
                if (string.IsNullOrEmpty(channelId) || channelId == "0")
                    qpTemp.Where = "";
            }

            if (string.IsNullOrEmpty(order)) //排序
            {
                qpTemp.Orderfld = " adddate desc ";
            }
            else
            {
                qpTemp.Orderfld = order;
            }
            if (cacheSecends > 0)
            {
                if (qpTemp.Where.IndexOf("adddate") > -1 || qpTemp.Where.IndexOf("toptime") > -1)
                {
                    qpTemp.IsTimeSearch = true;
                }
            }

           
            List<Module_ListInfo> List = MvcWebCacheManager.GetModuleList(type, qpTemp, cacheSecends, isReturnCountNum);
            if (isReturnCountNum)
                qp =(QueryParam) qpTemp.Clone();
            return List;

        }

        #endregion

        #region 广告
        /// <summary>
        /// 获取展示广告
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public Normal_AdvertiseEntity Advertise(string code)
        {
            Normal_AdvertiseEntity adv = null;
            adv = MvcWebCacheManager.GetNormal_Advertise(code);
            if (adv == null)
            {
                adv = new Normal_AdvertiseEntity();
            }
            return adv;
        }
        #endregion



       

        #region search

        

        /// <summary>
        /// 返回查询列表
        /// </summary>
        /// <param name="channelDir">查询频道Dir(为空查询所有)</param>
        /// <param name="pageSize">分页数</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="searchWord">搜索内容</param>
        /// <returns></returns>
        public List<Normal_SearchContentEntity> SearchList(int pageSize, int pageIndex, string searchWord)
        {

            qp.PageIndex = pageIndex;
            qp.PageSize = pageSize;

            qp.Where = searchWord;
            List<Normal_SearchContentEntity> list = new List<Normal_SearchContentEntity>();
            if (string.IsNullOrEmpty(searchWord))
            {
                return list;
            }
            LuceneHelper lucene = new LuceneHelper();

            list = lucene.Search(qp, 120);
            return list;
        }

        /// <summary>
        /// 文章相关列表
        /// </summary>
        /// <param name="articleId">文章id</param>
        /// <param name="searchContent">缓存搜索列表</param>
        /// <param name="searchUpdateTime">缓存时间</param>
       /// <param name="searchWord">搜索内容</param>
        /// <param name="channelPath">搜索频道Path</param>
       /// <param name="searchCount">搜索数量</param>
       /// <param name="saveHour">缓存时间</param>
        /// <param name="searchType">搜索类型1全部，2标题、tag，3tag</param>
       /// <returns></returns>
        public List<Module_ListInfo> SearchArticleList(int articleId, string searchContent, string searchUpdateTime, string searchWord, string channelPath, int searchCount, int saveHour, int searchType)
        {
            if (saveHour < 1)
                saveHour = 1;
            List<Module_ListInfo> lists = new List<Module_ListInfo>();
            if (string.IsNullOrEmpty(searchUpdateTime) || Utils.StrDateDiffHours(searchUpdateTime, 0) > saveHour)
            {
                Module_ArticleEntity ariticle = new Module_ArticleEntity();
                Module_Article bllArticle = new Module_Article();
                ariticle.Id = articleId;
                List<Module_ArticleEntity> articleList = MvcWebCacheManager.GetCacheModule_ArticleSearchList();
                if (articleList!=null &&  articleList.Contains(ariticle))
                {
                    for (int i = 0; i < articleList.Count; i++)
                    {
                        if (ariticle.Id == articleList[i].Id)
                        {
                            ariticle.SearchUpdateTime = articleList[i].SearchUpdateTime;
                            searchContent = ariticle.SearchContent = articleList[i].SearchContent;
                            break;

                        }
                    }
                }
                else
                {
                    lists = SearchPathList(channelPath, searchCount, 1, searchCount, searchWord, searchType);
                    if (lists.Count > 0)
                    {

                        if (lists[0].Title == searchWord)
                            lists.RemoveAt(0);
                        else
                        {
                            if (lists.Count == searchCount)
                                lists.RemoveAt(lists.Count - 1);
                        }

                        ariticle.SearchContent = JsonConvert.SerializeObject(lists, Formatting.Indented);
                        ariticle.SearchUpdateTime = DateTime.Now;
                        MvcWebCacheManager.SaveModule_ArticleSearchList(ariticle);

                    }
                }
            }

            if (!string.IsNullOrEmpty(searchContent))
            {
                if (searchContent.StartsWith("["))
                {
                    lists = (List<Module_ListInfo>)JsonConvert.DeserializeObject(searchContent, typeof(List<Module_ListInfo>), new JsonSerializerSettings());

                }
                else
                {
                    Module_ListInfo t = (Module_ListInfo)JsonConvert.DeserializeObject(searchContent, typeof(Module_ListInfo), new JsonSerializerSettings());
                    lists.Add(t);
                }
            }

            return lists;
        }

        /// <summary>
        /// 返回查询列表
        /// </summary>
        /// <param name="channelDir">查询频道Dir(为空查询所有)</param>
        /// <param name="pageSize">分页数</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="searchWord">搜索内容</param>
        /// <returns></returns>
        public List<Module_ListInfo> SearchPathList(string channelPath, int pageSize, int pageIndex, string searchWord)
        {


            qp.PageIndex = pageIndex;
            qp.PageSize = pageSize;
            qp.Where = searchWord;
            List<Module_ListInfo> list = new List<Module_ListInfo>();
            if (string.IsNullOrEmpty(searchWord))
            {
                return list;
            }
            LuceneHelper lucene = new LuceneHelper();

            list = lucene.Search(qp, channelPath, false);
           

            Normal_ChannelEntity channel2 = null;
            SiteConfigInfo siteInfo = SiteConfigs.GetSiteConfig();
            foreach (Module_ListInfo listInfo in list)
            {
                channel2 = new Normal_ChannelEntity();
                channel2 = MvcWebCacheManager.GetNormal_ChannelByPath(listInfo.Path);
                if (channel2 != null)
                {
                    listInfo.Url = string.Concat(channel2.DirectoryUrl, "/", (string.IsNullOrEmpty(siteInfo.ChannelUrl) ? listInfo.Id.ToString() : string.Format(siteInfo.ChannelUrl, listInfo.Id.ToString())));
                }
            }
            return list;
        }
       /// <summary>
        /// 返回查询列表
        /// </summary>
        /// <param name="channelDir">查询频道Dir(为空查询所有)</param>
        /// <param name="pageSize">分页数</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="searchWord">搜索内容</param>
       /// <param name="searchType">1全部，2标题、tag，3tag</param>
       /// <returns></returns>
        public List<Module_ListInfo> SearchPathList(string channelPath, int pageSize, int pageIndex,int searchCount, string searchWord,int searchType)
        {


            qp.PageIndex = pageIndex;
            qp.PageSize = pageSize;
            qp.Where = searchWord;
            List<Module_ListInfo> list = new List<Module_ListInfo>();
            if (string.IsNullOrEmpty(searchWord))
            {
                return list;
            }
            LuceneHelper lucene = new LuceneHelper();

            list = lucene.Search(qp, channelPath,searchCount,searchType);


            Normal_ChannelEntity channel2 = null;
            SiteConfigInfo siteInfo = SiteConfigs.GetSiteConfig();
            foreach (Module_ListInfo listInfo in list)
            {
                channel2 = new Normal_ChannelEntity();
                channel2 = MvcWebCacheManager.GetNormal_ChannelByPath(listInfo.Path);
                if (channel2 != null)
                {
                    listInfo.Url = string.Concat(channel2.DirectoryUrl, "/", (string.IsNullOrEmpty(siteInfo.ChannelUrl) ? listInfo.Id.ToString() : string.Format(siteInfo.ChannelUrl, listInfo.Id.ToString())));
                }
            }
            return list;
        }
        /// <summary>
        /// 返回查询列表
        /// </summary>
        /// <param name="channelDir">查询频道Dir(为空查询所有)</param>
        /// <param name="pageSize">分页数</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="searchWord">搜索内容</param>
        /// <returns></returns>
        public List<Module_ListInfo> SearchList(string channelDir, int pageSize, int pageIndex, string searchWord)
        {
           
          
            qp.PageIndex = pageIndex;
            qp.PageSize = pageSize;
            qp.Where = searchWord;
            List<Module_ListInfo> list = new List<Module_ListInfo>();
            if (string.IsNullOrEmpty(searchWord))
            {
                return list;
            }
            LuceneHelper lucene = new LuceneHelper();
            if (!string.IsNullOrEmpty(channelDir))
            {
                Normal_ChannelEntity channelEntity = MvcWebCacheManager.GetNormal_ChannelByDir(channelDir);

                if (channelEntity == null)
                {
                    return list;
                }
                list = lucene.Search(qp, channelEntity.Path,false);
            }
            else
            {
                list = lucene.Search(qp, string.Empty, false);
            }
         
            Normal_ChannelEntity channel2 = null;
            SiteConfigInfo siteInfo = SiteConfigs.GetSiteConfig();
            foreach (Module_ListInfo listInfo in list)
            {
                channel2 = new Normal_ChannelEntity();
                channel2 =MvcWebCacheManager.GetNormal_ChannelByPath(listInfo.Path);
                if (channel2 != null)
                {
                    listInfo.Url = string.Concat(channel2.DirectoryUrl, "/", (string.IsNullOrEmpty(siteInfo.ChannelUrl) ? listInfo.Id.ToString() : string.Format(siteInfo.ChannelUrl, listInfo.Id.ToString())));
                }
            }
            return list;
        }

        /// <summary>
        /// 返回查询列表
        /// </summary>
        /// <param name="channelDir">查询频道Dir(为空查询所有)</param>
        /// <param name="pageSize">分页数</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="searchWord">搜索内容</param>
        /// <returns></returns>
        public List<Module_ListInfo> SearchList(int channelId, int pageSize, int pageIndex, string searchWord, int searchCount, bool isSearchTitle)
        {

            int searchType = 0;
            if (isSearchTitle)
            {
                searchType = 2;
            }
            else
                searchType = 1;
            return SearchList(channelId, pageSize, pageIndex, searchWord, searchCount, searchType);
        }
        /// <summary>
        /// 返回查询列表
        /// </summary>
        /// <param name="channelDir">查询频道Dir(为空查询所有)</param>
        /// <param name="pageSize">分页数</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="searchWord">搜索内容</param>
        /// <returns></returns>
        public List<Module_ListInfo> SearchList(int channelId, int pageSize, int pageIndex, string searchWord, int searchCount, int searchType )
        {

            qp.PageIndex = pageIndex;
            qp.PageSize = pageSize;
            qp.Where = searchWord;
            List<Module_ListInfo> list = new List<Module_ListInfo>();
            if (string.IsNullOrEmpty(searchWord))
            {
                return list;
            }
            LuceneHelper lucene = new LuceneHelper();
           
            if (channelId != 0)
            {
                Normal_ChannelEntity channelEntity = MvcWebCacheManager.GetNormal_Channel(channelId);

                if (channelEntity == null)
                {
                    return list;
                }
                list = lucene.Search(qp, channelEntity.Path, searchCount, searchType);
            }
            else
            {
                list = lucene.Search(qp, string.Empty, searchCount, searchType);
            }
            SiteConfigInfo siteInfo = SiteConfigs.GetSiteConfig();
            Normal_ChannelEntity channel2 = null; ;
            foreach (Module_ListInfo listInfo in list)
            {
                channel2 = new Normal_ChannelEntity();
                channel2 = MvcWebCacheManager.GetNormal_ChannelByPath(listInfo.Path);
                if (channel2 != null)
                {
                    listInfo.Url = string.Concat(channel2.DirectoryUrl, "/", (string.IsNullOrEmpty(siteInfo.ChannelUrl) ? listInfo.Id.ToString() : string.Format(siteInfo.ChannelUrl, listInfo.Id.ToString())));
                }
            }
            return list;
        }

        /// <summary>
        /// 搜索内容列表
        /// </summary>
        /// <param name="reNum"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strWhere"></param>
        /// <param name="orderby"></param>
        /// <param name="cacheNum"></param>
        /// <returns></returns>
        public List<Normal_SearchContentEntity> SearchContentList(int pageSize, int pageIndex, string strWhere, string orderby, int cacheNum)
        {
            qp.ReturnFields = "";
            qp.PageSize = pageSize;
            qp.PageIndex = pageIndex;
            qp.Where = strWhere;
            qp.Orderfld = orderby;
            return MvcWebCacheManager.GetNormal_SearchContentList(qp, cacheNum);
        }
        #endregion


        #region keyword
        public List<Normal_KeywordEntity> GetKeyWordList(string strWhere,string orderby ,int reNum,int cacheNum)
        {
            QueryParam qp = new QueryParam();
            qp.PageSize = reNum;
            qp.Where = strWhere;
            qp.Orderfld = orderby;
            return MvcWebCacheManager.GetNormal_KeywordList(qp, cacheNum);
        }
        #endregion 
        /// <summary>
        /// 返回指定长度的字符串,区分汉字的
        /// </summary>
        /// <param name="str">要测试的字符串</param>
        /// <param name="length">返回长度</param>
        /// <param name="isShowEllipsis">是否加省略号</param>
        /// <returns></returns>
        public string GetLengthString(string str, int returnLength, bool isShowEllipsis)
        {
            return Utils.GetLengthString(str, returnLength, isShowEllipsis);
        }

        #region 专题
        /// <summary>
        /// 获取专题列表
        /// </summary>
        /// <param name="channelPath"></param>
        /// <param name="orderBy"></param>
        /// <param name="pageSize"></param>
        /// <param name="cacheNum"></param>
        /// <returns></returns>
        public List<Normal_ZTEntity> ZTList(string channelPath, string orderBy, int pageSize, int cacheNum)
        {
            List<Normal_ZTEntity> ztList = new List<Normal_ZTEntity>();
            qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = pageSize;
            qp.Where = "Enabled=1 ";
            if (!string.IsNullOrEmpty(channelPath))
                qp.Where = string.Concat(qp.Where," and channelPath='", channelPath, "'");
            if (string.IsNullOrEmpty(orderBy))
            {
                qp.Orderfld = " orderNum desc ";
            }
            else
                qp.Orderfld = orderBy;
            ztList = MvcWebCacheManager.GetZTList(qp, cacheNum);
            return ztList;
        }
        #endregion 


    }

    public class PagingInfo
    {
        public string Href { get; set; }

        public string Title { get; set; }

    }
}